package cn.jasonone.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.jasonone.bean.SysResourceType;
import cn.jasonone.service.SysResourceTypeService;

@RestController
@RequestMapping("/resource-type")
public class SysResourceTypeController {
	@Resource
	private SysResourceTypeService sysResourceTypeService;

	@GetMapping
	public ModelAndView home() {
		return new ModelAndView("sys/resourcetype/index");
	}

	@GetMapping({ "/save", "/save/{id}" })
	public ModelAndView save(@PathVariable(name = "id", required = false) Integer id) {
		ModelAndView modelAndView = new ModelAndView("sys/resourcetype/save");
		if (id != null) {
			modelAndView.addObject("resourcetype", sysResourceTypeService.findById(id));
		}
		return modelAndView;
	}

	@PostMapping("/save")
	public Object save(SysResourceType resourcetype) {
		Map<String, Object> result = new HashMap<>();
		result.put("success", sysResourceTypeService.save(resourcetype));
		return result;
	}

	@GetMapping("/find")
	public Object findAll(SysResourceType resourcetype,
			@RequestParam(defaultValue = "1")int page,
			@RequestParam(defaultValue = "10")int limit
			) {
		PageHelper.startPage(page, limit);
		List<SysResourceType> list = sysResourceTypeService.findAll(resourcetype);
		PageInfo<SysResourceType> pageInfo=new PageInfo<>(list);
		Map<String,Object> result=new HashMap<>();
		result.put("code", 0);
		result.put("msg", "查询成功");
		result.put("count", pageInfo.getTotal());
		result.put("data", pageInfo.getList());
		return result;
	}
	@GetMapping("/del")
	public Object delete(int[] ids) {
		Map<String,Object> result=new HashMap<>();
		result.put("success", true);
		if(ids == null || ids.length==0) {
			result.put("success", false);
			result.put("msg", "非法的参数");
		}
		if(!this.sysResourceTypeService.deleteByIds(ids)) {
			result.put("success", false);
			result.put("msg", "删除失败或数据不存在");
		}
		return result;
	}
	
}
